package com.mars.admin.modules.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.mars.admin.common.request.chat.CreateSessionRequest;
import com.mars.admin.framework.common.Result;
import com.mars.admin.modules.chat.entity.ChatMessage;
import com.mars.admin.modules.chat.entity.ChatSession;
import com.mars.admin.modules.chat.service.ChatService;
import com.mars.admin.modules.chat.vo.ChatSessionVO;
import com.mars.admin.modules.chat.vo.ChatSessionWithUsersVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天控制器
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@RestController
@RequestMapping("/api/chat")
@Tag(name = "聊天管理", description = "聊天相关接口")
public class ChatController {

    @Autowired
    private ChatService chatService;

    @PostMapping("/session/create")
    @Operation(summary = "创建或获取聊天会话")
    public Result<ChatSessionWithUsersVO> createSession(@RequestBody CreateSessionRequest request) {
        Long currentUserId = StpUtil.getLoginIdAsLong();
        ChatSessionWithUsersVO session = chatService.getOrCreateSession(currentUserId, request.getTargetUserId());
        return Result.success(session);
    }

    @GetMapping("/sessions")
    @Operation(summary = "获取用户的会话列表", description = "获取当前用户的所有聊天会话，包含对方用户的头像和昵称信息")
    public Result<List<ChatSessionVO>> getUserSessions() {
        Long currentUserId = StpUtil.getLoginIdAsLong();
        List<ChatSessionVO> sessions = chatService.getUserSessionsWithUserInfo(currentUserId);
        return Result.success(sessions);
    }

    @GetMapping("/messages/{sessionId}")
    @Operation(summary = "获取会话消息列表")
    public Result<List<ChatMessage>> getSessionMessages(
            @PathVariable Long sessionId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        List<ChatMessage> messages = chatService.getSessionMessages(sessionId, page, size);
        return Result.success(messages);
    }

    @PostMapping("/messages/read/{sessionId}")
    @Operation(summary = "标记消息为已读")
    public Result<Void> markMessagesAsRead(@PathVariable Long sessionId) {
        Long currentUserId = StpUtil.getLoginIdAsLong();
        chatService.markMessagesAsRead(sessionId, currentUserId);
        return Result.success();
    }

    @GetMapping("/messages/unread/{sessionId}")
    @Operation(summary = "获取未读消息数")
    public Result<Integer> getUnreadCount(@PathVariable Long sessionId) {
        Long currentUserId = StpUtil.getLoginIdAsLong();
        Integer count = chatService.getUnreadCount(sessionId, currentUserId);
        return Result.success(count);
    }

    @DeleteMapping("/session/{sessionId}")
    @Operation(summary = "删除会话")
    public Result<Void> deleteSession(@PathVariable Long sessionId) {
        Long currentUserId = StpUtil.getLoginIdAsLong();
        chatService.deleteSession(sessionId, currentUserId);
        return Result.success();
    }
}
